package com.kedeng.yangmuyi.controller;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.kedeng.yangmuyi.dto.LoginUserDTO;
import com.kedeng.yangmuyi.dto.UserProfileDTO;
import com.kedeng.yangmuyi.service.LoginService;

/**
 * 
 * Handle user login request, if login success, redirect to login success page,
 * if failed, redirect to register page.
 * 
 * @author: rqian
 * @version: 1.0, Apr 26, 2012
 */
@Controller
public class LoginController extends AbstractController {
    
    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Resource
    private LoginService       loginService;



    /**
     * 
     * checking login user information, if validation is correct, then redirect
     * to loginSuccess page, or redirect to register page.
     * 
     * @param user
     * @param model
     * @return
     */
    @RequestMapping(value = "/login.do", method = RequestMethod.POST)
    public ModelAndView checkLogin(@Validated @ModelAttribute("loginUser")
    LoginUserDTO user, Model model, BindingResult result) {
        logger.debug("LoginController.checkLogin():enter");
        logger.debug("LoginController.checkLogin() parameter - loginUser: " + user);
        
        if (result.hasErrors()) {
            model.addAttribute("loginUser", new LoginUserDTO());
            return new ModelAndView("welcome");
        }
        boolean isloginSuccess = loginService.checkLoginUserData(user);

        if (isloginSuccess) {
            return new ModelAndView("loginSuccess", "user", user);
        } else {
            model.addAttribute("registUser", new UserProfileDTO());
            return new ModelAndView("register");
        }
    }



    /**
     * 
     * register user.
     * 
     * @param user
     * @param model
     * @return
     */
    @RequestMapping(value = "/register.do", method = RequestMethod.POST)
    public ModelAndView registerUser(@ModelAttribute("loginUser")
    UserProfileDTO profile, Model model) {
        boolean registerUserSuccess = loginService.registerUser(profile);
        if (registerUserSuccess) {
            return new ModelAndView("loginSuccess", "user", profile);
        } else {
            return new ModelAndView("register");
        }
    }



    /**
     * @return the loginService
     */
    public LoginService getLoginService() {
        return loginService;
    }



    /**
     * @param loginService
     *            the loginService to set
     */
    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }

}
